package com.hjwoo.app.views;


import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.Date;
import com.hjwoo.app.R;
import com.hjwoo.app.adpter.TimeAdapter;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.utils.MyTimeUtils;
import com.hjwoo.app.utils.Utils;

import java.util.ArrayList;


/**
 * Create by ：rxiaowen on 2016/8/3
 * Email：rxiaowen@sina.com
 * 获取日期界面
 */

public class GetDateActivity extends BaseAppCompatActivity {
    private ArrayList<String> dayList = new ArrayList<>();
    private ListView lv_data;
    private TimeAdapter timeAdapter;
    public static final int DATE = 8;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_date);
        Utils.initTitle("预约日期", this, true);
        lv_data = (ListView) findViewById(R.id.lv_data);
        findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //获取系统日期
        String str =  MyTimeUtils.getSystemDate();
        //将String字符串设置成Data格式
        Date date =  MyTimeUtils.str2Date(str);
        for (int i = 2 ; i < 7; i++){
            Date afterDays = MyTimeUtils.addDays(date,i);//加几天，返回几天后的时间
            String afterDate = MyTimeUtils.Date2String(afterDays);
            dayList.add(afterDate);
        }
        timeAdapter = new TimeAdapter(dayList,GetDateActivity.this);
        lv_data.setAdapter(timeAdapter);
        lv_data.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //返回数据
                Intent intent = getIntent();
                String tv_Time = dayList.get(position);
                intent.putExtra("date",tv_Time);
                GetDateActivity.this.setResult(DATE, intent);
                GetDateActivity.this.finish();
            }
        });
    }
    //监听手机返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ){
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
